/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visao;

import Visao.Funcionario.InserirAlterarCliente;
import Visao.Funcionario.GerenciarClientes;
import Controle.Controlador;
import Modelo.Acompanhamento;
import Modelo.Pizza;
import Visao.Funcionario.Pedido.AcompanharPedido;
import Visao.Funcionario.Pedido.EditarPedido;
import Visao.Funcionario.Pedido.ReceberPagamento;
import Visao.Funcionario.Pedido.SelecionarClientePedido;
import Visao.Gerente.AreaAdministrador;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Eder
 */
public class TelaPrincipal extends javax.swing.JFrame {
    DefaultTableModel modelo1;
    DefaultTableModel modelo2;
    DefaultTableModel modelo3;
    ArrayList<ArrayList<String>> listaPedidos;
    /**
     * Creates new form TelaPrincipal
     */
    public TelaPrincipal() {
        initComponents();
        modelo1 = (DefaultTableModel) jtabelaPedido.getModel();
        modelo2 = (DefaultTableModel) jtTabelaPizzas.getModel();
        modelo3 = (DefaultTableModel) jtTabelaAcomp.getModel();
        jScrollPane1.getViewport().setBackground(Color.WHITE);
        jScrollPane2.getViewport().setBackground(Color.WHITE);
        jScrollPane4.getViewport().setBackground(Color.WHITE);
        jtabelaPedido.getColumnModel().getColumn( 0 ).setMaxWidth( 0 );  
        jtabelaPedido.getColumnModel().getColumn( 0 ).setMinWidth( 0 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 0 ).setMaxWidth( 0 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 0 ).setMinWidth( 0 );
        jtabelaPedido.getColumnModel().getColumn( 1 ).setMaxWidth( 1 );  
        jtabelaPedido.getColumnModel().getColumn( 1 ).setMinWidth( 1 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 1 ).setMaxWidth( 1 );  
        jtabelaPedido.getTableHeader().getColumnModel().getColumn( 1 ).setMinWidth( 1 );
        jtabelaPedido.getColumnModel().getColumn( 2 ).setMaxWidth( 170 );  
        jtabelaPedido.getColumnModel().getColumn( 2 ).setMinWidth( 170 );
        jtTabelaPizzas.getColumnModel().getColumn( 2 ).setMaxWidth( 100 );
        jtTabelaPizzas.getColumnModel().getColumn( 2 ).setMinWidth( 100 );
        jtTabelaAcomp.getColumnModel().getColumn( 1 ).setMaxWidth( 100 );
        jtTabelaAcomp.getColumnModel().getColumn( 1 ).setMinWidth( 100 );
        Controlador.conectarBancoDados();
        imprimeDadosTabelas();
    }
    
    private boolean carregarDadosPedidos_BD(){
        try {
            listaPedidos = Controlador.carregarDadosBD_Pedidos();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Carregar os dados dos Pedido no Banco de Dados!\n"+e.getMessage(), "Erro!", 0);
            return false;
        }
    }
    
    private void imprimeDadosTabelas() {
        int linhaTabela = 0;
        
        //jTtabelaRP.setRowSorter(null); // desabilita o filtro
        carregarDadosPedidos_BD();
        if(listaPedidos!=null){
            for (int i = 0; i < listaPedidos.size(); i++) {            
                if (jtabelaPedido.getRowCount() <= linhaTabela) {
                    modelo1.addRow(new String[]{"", ""});
                }
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(0), linhaTabela, 0);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(1), linhaTabela, 1);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(2), linhaTabela, 2);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(3), linhaTabela, 3);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(4), linhaTabela, 4);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(5), linhaTabela, 5);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(6), linhaTabela, 6);
                jtabelaPedido.setValueAt(listaPedidos.get(i).get(7), linhaTabela, 7);

                linhaTabela++;
            }
            removeLinhasInutilizadas(linhaTabela);
        }
    }
    
    
    public void atualizaTabela(){
        imprimeDadosTabelas();
        jtabelaPedidoMouseClicked(null);
    }
    
    public void removeLinhasInutilizadas(int linhaTabela){
        for (int i = linhaTabela; i < jtabelaPedido.getRowCount(); i++) {
            modelo1.removeRow(i);
        }
    }
    
    private void imprimeDadosTabelaPizzas(ArrayList<ArrayList<String>> pizzas) {
        int linhaTabela = 0;
        try {
            for (int i = 0; i < pizzas.size(); i++) {            
//                if (jtTabelaPizzas.getRowCount() <= linhaTabela) {
//                    modelo2.addRow(new String[]{"", ""});
//                }
                modelo2.addRow(new String[]{"", ""});
                jtTabelaPizzas.setValueAt(pizzas.get(i).get(0), linhaTabela, 0);
                jtTabelaPizzas.setValueAt(pizzas.get(i).get(1), linhaTabela, 1);
                jtTabelaPizzas.setValueAt(pizzas.get(i).get(2), linhaTabela, 2);

                linhaTabela++;
            }
//            removeLinhasInutilizadasTabelaPizzas(linhaTabela);
        } catch (Exception e) {
            System.out.println("Erro ao Imprimir dados na tabela Pizzas!\n"+ e.getMessage());
        }
    }
    
    public void removeLinhasTabelaPizzas(){
        for (int i = 0; i < jtTabelaPizzas.getRowCount(); i++) {
            modelo2.removeRow(i);
        }
    }
    
    private void imprimeDadosTabelaAcomp(ArrayList<ArrayList<String>> acomp) {
        int linhaTabela = 0;
        try {
            for (int i = 0; i < acomp.size(); i++) {            
//                if (jtTabelaAcomp.getRowCount() <= linhaTabela) {
//                    modelo3.addRow(new String[]{"", ""});
//                }
                modelo3.addRow(new String[]{"", ""});
                
                jtTabelaAcomp.setValueAt(acomp.get(i).get(1), linhaTabela, 0);
                jtTabelaAcomp.setValueAt(acomp.get(i).get(2), linhaTabela, 1);

                linhaTabela++;
            }
//            removeLinhasInutilizadasTabelaAcomp(linhaTabela);
        } catch (Exception e) {
            System.out.println("Erro ao Imprimir dados na tabela de Acompanhamestos do Pedido!\n"+ e.getMessage());
        }
    }
    
    public void removeLinhasTabelaAcomp(){
        for (int i = 0; i < jtTabelaAcomp.getRowCount(); i++) {
            modelo3.removeRow(i);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator3 = new javax.swing.JSeparator();
        jbFinalizar = new javax.swing.JButton();
        jbGerarNota = new javax.swing.JButton();
        jbEditar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jNovoPedido = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtabelaPedido = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jbPagar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtTabelaAcomp = new javax.swing.JTable();
        jbGerenciarClientes = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jtTabelaPizzas = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jbAcompanharPedido = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SISEP - Sistema de Entrega de Pizzas");
        setAutoRequestFocus(false);
        setResizable(false);

        jbFinalizar.setText("Finalizar");
        jbFinalizar.setToolTipText("Finalizar Pedido");
        jbFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbFinalizarActionPerformed(evt);
            }
        });

        jbGerarNota.setText("Gerar Nota");
        jbGerarNota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGerarNotaActionPerformed(evt);
            }
        });

        jbEditar.setText("Editar");
        jbEditar.setToolTipText("Editar Pedido");
        jbEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEditarActionPerformed(evt);
            }
        });

        jbCancelar.setText("Cancelar");
        jbCancelar.setToolTipText("Cancelar Pedido");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jNovoPedido.setText("<html><CENTER>Novo<br>Pedido");
        jNovoPedido.setPreferredSize(new java.awt.Dimension(80, 75));
        jNovoPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNovoPedidoActionPerformed(evt);
            }
        });

        jButton3.setText("<html> <CENTER> Área do<br>Gerente");
        jButton3.setToolTipText("Área do Gerente");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("<html> <CENTER> Alterar<br>Status");
        jButton4.setToolTipText("Alterar Status");
        jButton4.setPreferredSize(new java.awt.Dimension(80, 75));

        jtabelaPedido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Ped", "Código Cliente", "Nome", "Entrega", "Tx Entrega", "Desconto", "Valor", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtabelaPedido.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtabelaPedidoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtabelaPedido);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Pedido Selecionado");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Pedidos");

        jbPagar.setText("Pagar");
        jbPagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPagarActionPerformed(evt);
            }
        });

        jtTabelaAcomp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Produto", "Valor"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtTabelaAcomp.setPreferredSize(new java.awt.Dimension(255, 100));
        jScrollPane2.setViewportView(jtTabelaAcomp);

        jbGerenciarClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/1313784489_client.png"))); // NOI18N
        jbGerenciarClientes.setToolTipText("Gerenciar Clientes");
        jbGerenciarClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGerenciarClientesActionPerformed(evt);
            }
        });

        jtTabelaPizzas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Sabores", "Tamanho", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtTabelaPizzas.setPreferredSize(new java.awt.Dimension(255, 100));
        jScrollPane4.setViewportView(jtTabelaPizzas);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Pizza Pedido");

        jbAcompanharPedido.setText("Acompanhar Pedido");
        jbAcompanharPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAcompanharPedidoActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Acompanhamento Pedido");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jbCancelar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbEditar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbFinalizar))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addGap(9, 9, 9))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 121, Short.MAX_VALUE)
                                        .addComponent(jbAcompanharPedido)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jbGerarNota)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jbPagar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jNovoPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(jbGerenciarClientes, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(56, 56, 56)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator3)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbGerenciarClientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jNovoPedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbPagar)
                        .addComponent(jbGerarNota)
                        .addComponent(jbAcompanharPedido))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbCancelar)
                        .addComponent(jbEditar)
                        .addComponent(jbFinalizar))
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(116, 116, 116))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-933)/2, (screenSize.height-511)/2, 933, 511);
    }// </editor-fold>//GEN-END:initComponents

    private void jNovoPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNovoPedidoActionPerformed
        // Novo Pedido:
        SelecionarClientePedido clientes = new SelecionarClientePedido(this);
        clientes.setVisible(true);
    }//GEN-LAST:event_jNovoPedidoActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        new AreaAdministrador().setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jbGerenciarClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGerenciarClientesActionPerformed
        // Gerenciar GerenciarClientes
        GerenciarClientes clientes = new GerenciarClientes();
        clientes.setVisible(true);
    }//GEN-LAST:event_jbGerenciarClientesActionPerformed

    private void jtabelaPedidoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtabelaPedidoMouseClicked
        // Ao Clicar na tabela Pedidos
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                ArrayList<ArrayList<String>> pizzas = Controlador.carregarDadosBD_ItemPizzaPedido(codigoPedido);
                ArrayList<ArrayList<String>> acomp = Controlador.carregarDadosBD_ItemAcompanhentoPedido(codigoPedido);
                removeLinhasTabelaPizzas();
                removeLinhasTabelaAcomp();
                
                if(pizzas != null){
                    imprimeDadosTabelaPizzas(pizzas);
                }
                if(acomp!= null){
                    imprimeDadosTabelaAcomp(acomp);
                }
                
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            System.out.println("Erro "+e.getMessage());
            //JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jtabelaPedidoMouseClicked

    private void jbEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEditarActionPerformed
        // Editar Pedido
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                EditarPedido ped = new EditarPedido(this, codigoPedido);
                ped.setVisible(true);
                
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jbEditarActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        // Cancelar Pedido
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int op = JOptionPane.showConfirmDialog(rootPane, "Tem Certeza que Deseja Cancelar o Pedido Selecionado?", "Cancelar Pedido", 0, 2);
                if(op==0){
                    int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                    Controlador.cancelar_Pedido(codigoPedido);
                    atualizaTabela();
                }   
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Cancelar Pedido!\n"+e.getMessage(), "Cancelar Pedido", 0);
        }
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbFinalizarActionPerformed
        // Finalizar Pedido
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int op = JOptionPane.showConfirmDialog(rootPane, "Tem Certeza que Deseja Finalizar o Pedido Selecionado?", "Finalizar Pedido", 0, 2);
                if(!"Pago".equalsIgnoreCase( jtabelaPedido.getValueAt(linha, 7).toString())){
                    throw new Exception("O Pedido deve ser Pago antes de ser Finalizado!");
                }
                if(op==0){
                    int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                    Controlador.finalizar_Pedido(codigoPedido);
                    atualizaTabela();
                }   
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Finalizar Pedido!\n"+e.getMessage(), "Finalizar Pedido", 0);
        }
    }//GEN-LAST:event_jbFinalizarActionPerformed

    private void jbPagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPagarActionPerformed
        // Pagar                
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                Float valor = Float.parseFloat(jtabelaPedido.getValueAt(linha, 6).toString());
                ReceberPagamento receberPag = new ReceberPagamento(this, codigoPedido, valor);
                receberPag.setVisible(true);
                
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jbPagarActionPerformed

    private void jbAcompanharPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAcompanharPedidoActionPerformed
        
        // Acompanhar Pedido
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                int codigoCliente = Integer.parseInt(jtabelaPedido.getValueAt(linha, 1).toString());
                float txEntrega = Float.parseFloat(jtabelaPedido.getValueAt(linha, 4).toString());
                float desc = Float.parseFloat(jtabelaPedido.getValueAt(linha, 5).toString());
                float total = Float.parseFloat(jtabelaPedido.getValueAt(linha, 6).toString());
                AcompanharPedido ped = new AcompanharPedido(this, codigoPedido,codigoCliente, txEntrega, desc, total);
                ped.setVisible(true);
                
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jbAcompanharPedidoActionPerformed

    private void jbGerarNotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGerarNotaActionPerformed
        // Botão Gerar Nota
        try {
            int linha = jtabelaPedido.getSelectedRow();
            if (linha != -1) {
                int codigoPedido = Integer.parseInt(jtabelaPedido.getValueAt(linha, 0).toString());
                
                
            } else {
                throw new Exception("Selecione a linha do Pedido desejado");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }//GEN-LAST:event_jbGerarNotaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SelecionarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SelecionarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SelecionarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SelecionarCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jNovoPedido;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton jbAcompanharPedido;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbFinalizar;
    private javax.swing.JButton jbGerarNota;
    private javax.swing.JButton jbGerenciarClientes;
    private javax.swing.JButton jbPagar;
    private javax.swing.JTable jtTabelaAcomp;
    private javax.swing.JTable jtTabelaPizzas;
    private javax.swing.JTable jtabelaPedido;
    // End of variables declaration//GEN-END:variables
}
